﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using LED_Editor.ControllerProgramming;


namespace LED_ControllerEmulator
{
    public partial class TestControllerEmulator : Form
    {
        public TestControllerEmulator()
        {
            InitializeComponent();
        }


        private void TestControllerProgramming_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] values = maskedTextBox1.Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (values.Length != 2)
                return;

            int address = int.Parse(values[0]);
            char command = Convert.ToChar(values[1]);

            try
            {
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();
                    serialPort1.Write(new byte[] { (byte)address, Convert.ToByte(command) }, 0, 2);
                }
            }
            finally
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
            }
        }

        Query echoResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.ECHO, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query enterProgrammingResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.ENTER_PROG_MODE, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query writeFrameResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.WRITE_FRAME, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query finalizeResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.FINILIZE_PROG, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query setColorResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.SET_COLOR, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query playResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.RUN_PROGRAM, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query pauseResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.PAUSE_PROGRAM, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query resumeResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.RESUME_PROGRAM, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query brightResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.CHANGE_BRIGHTNESS, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query speedResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)1, QueryCodes.CHANGE_SPEED, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query startSyncResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)0, QueryCodes.START_SYNC, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);
        Query stopSyncResponse = Query.CreateInstance(Query.StartByte, QueryType.Confirm, (byte)0, QueryCodes.STOP_SYNC, (byte)1, ErrorCodes.NO_ERROR, (byte)0, Query.EndByte);

        void Send(Query query)
        {
            try
            {
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();
                    serialPort1.Write(query.ToByteArray() , 0, 8);
                }
            }
            finally
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
            }
        }

        private void btnEcho_Click(object sender, EventArgs e)
        {
            Send(echoResponse);
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            Send(enterProgrammingResponse);
        }

        private void btnWriteFrame_Click(object sender, EventArgs e)
        {
            Send(writeFrameResponse);
        }

        private void btnFinalize_Click(object sender, EventArgs e)
        {
            Send(finalizeResponse);
        }

        private void btnSetColor_Click(object sender, EventArgs e)
        {
            Send(setColorResponse);
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            Send(playResponse);
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            Send(pauseResponse);
        }

        private void btnSpeed_Click(object sender, EventArgs e)
        {
            Send(speedResponse);
        }

        private void btnBright_Click(object sender, EventArgs e)
        {
            Send(brightResponse);
        }

        private void btnResume_Click(object sender, EventArgs e)
        {
            Send(resumeResponse);
        }

        private void btnStartSync_Click(object sender, EventArgs e)
        {
            Send(startSyncResponse);
        }

        private void btnStopSync_Click(object sender, EventArgs e)
        {
            Send(stopSyncResponse);
        }


   
    }
}
